package game.com;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Environment;

public class GameResource {
	
	// Resource folder in SD Card
	private String resourceFolder;
	private final String projectName = "working";
	
	private AssetManager assetManager;
	
	public GameResource(Resources resources) {
		assetManager = resources.getAssets();
	}

	// Init and check if resource folder is available
	public void initResource()
	{
		//make company name
		resourceFolder = Environment.getExternalStorageDirectory() + "/vietgameplay";
		File companyFolderName = new File(resourceFolder);
		if (!companyFolderName.exists())
		{
			companyFolderName.mkdir();
		}
		//make project name
		resourceFolder = resourceFolder + "/" + projectName;
		File resourceFolderFile = new File(resourceFolder);
		if (!resourceFolderFile.exists())
		{
			resourceFolderFile.mkdir();
		}
	}
	
	// Create resource folder
	public void mapResource(String fileName) throws IOException
	{
		File resourceFile = new File(resourceFolder + "/" + fileName);
		if (!resourceFile.exists())
		{
			InputStream inputStream = assetManager.open(fileName);
			
			FileOutputStream outputStream = new FileOutputStream(resourceFile);
			byte buffer[] = new byte[1024];
			int length = 0;
			while (inputStream.available() > 0)
			{
				length = inputStream.read(buffer);
				outputStream.write(buffer, 0, length);
			}
		}	
	}
}
